function duplicateNode(link){
	var node = $(link).parent().parent();
	var list = node.parent();
	var new_node = node.clone();
	list.append(new_node);
}

function createNode(){
	var link = arguments[0];
	var clear_children_id = arguments[1] ? arguments[1] : '';
	
	var node = $(link).parent().parent();
	var list = node.parent();
	var new_node = node.clone();
	new_node.find('input').val('');
	
	if(clear_children_id != ''){
		var children = clear_children_id.split(',');
		for(var i = 0; i < children.length; i++){
			new_node.children(':eq(' + children[i] + ')').html('');
		}
	}
	
	list.append(new_node);
}

function popupMsg(){
	var parent = arguments[0] ? arguments[0] : '';
	var message = arguments[1] ? arguments[1] : '';
	
	$('#popup_tinymsg').hide();
	
	if(message != ''){
		$('#popup_tinymsg')
			.html(message)
			.css('top', parent.offset().top + parent.outerHeight() + 2)
			.css('left', parent.offset().left)
			.fadeIn();
	}
}

function removeNode(link){
	var node = $(link).parent().parent();
	node.remove();
}

function createFlashObject(src){
	return $('<embed src="' + src + '" type="application/x-shockwave-flash" wmode="transparent"></embed>');
}

function showMessage(){
	var message = '';
	var redirect_url = '';
	
	if(typeof arguments[0] == 'object'){
		message = arguments[0].message;
		redirect_url = arguments[0].redirect_url;
	}else if(typeof arguments[0] == 'string' && arguments[0].charAt(0) == '{'){
		eval('var obj = ' + arguments[0] + ';');
		message = obj.message;
		redirect_url = obj.redirect_url;
	}
	
	$('#popup_message .content').html(message);
	$('#popup_message .operation').html('');
	
	var top = ($(window).height() - $('#popup_message').outerHeight()) / 2 + $(window).scrollTop();
	var left = ($(window).width() - $('#popup_message').outerWidth()) / 2 + $(window).scrollLeft();
	$('#popup_message').css('top', top);
	$('#popup_message').css('left', left);
	$('#popup_message').animate({'opacity':'show','top':'-=80px'}, 1000);

	if(redirect_url.length > 0){
		var timeout = 1000 * parseInt(Config.messageTimeout, 10);
		if(redirect_url == 'back'){
			setTimeout('$("#popup_message").fadeOut()', timeout);
		}else if(redirect_url == 'refresh'){
			setTimeout('location.reload()', timeout);
		}else if(redirect_url == 'confirm'){
			$('#popup_message .operation').html('<a href="' + obj.request_url + '&confirm=true">[Ok, just go on!]</a> <a href="###" onclick="$(\'#popup_message\').fadeOut()">[No, cancel it please.]</a>');
		}else{
			setTimeout('location.href="' + redirect_url + '";', timeout);
		}
	}
}

function secondTimeFormat(second){
	var hour = Math.floor(second / 3600);
	second %= 3600;
	var min = Math.floor(second / 60);
	second %= 60;
	
	var date = '';
	if(hour > 0){
		if(hour < 10) date += '0';
		date += hour + ':';
	}
	if(min > 0){
		if(min < 10) date += '0';
		date += min + ':';
	}
	if(second < 10){
		date += '0';
	}
	date += second;
	return date;
}

$(function(){
	var popup_message = $('<div id="popup_message" class="messagebox popup"><h1>+Quiz System Message</h1><div class="content"></div><div class="operation"></div></div>');
	$('#wrap').append(popup_message);
	var popup_tinymsg = $('<div id="popup_tinymsg" class="tinymsg popup"></div>');
	$('#wrap').append(popup_tinymsg);

	$('.popupmenu').mouseenter(function(){
		var menu_id = '#' + this.id + '_menu';
		if($(menu_id).is(":visible") == false){
			$('.popup').fadeOut();
			$(menu_id).css('top', $(this).offset().top + $(this).outerHeight());
			$(menu_id).css('left', $(this).offset().left);
			$(menu_id + ' li:odd').css({position:'relative',left:'30px'});
			$(menu_id + ' li:even').css({position:'relative',left:'-30px'});
			$(menu_id).fadeIn();
			$(menu_id + ' li').animate({left:'0'});
		}
	});

	$('.popupmenu').mouseleave(function(){
		var menu_id = '#' + this.id + '_menu';
		$(menu_id + ' li:odd').animate({left:'-30px'});
		$(menu_id + ' li:even').animate({left:'30px'});
		$(menu_id).fadeOut();
	});
	
	$('#popup_message').unbind('mouseleave');
	$('#popup_tinymsg').unbind('mouseleave');
	
	$(document).click(function(){
		$('#popup_message').fadeOut();
	});
});
